﻿using System.Configuration;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace ZhuGe.Demo.Form.CSFile
{
    [Table]
    public class TestTable
    {
        [Column(IsPrimaryKey = true, IsDbGenerated = true)]
        public int Id;

        [Column(CanBeNull = false)]
        public string Key;

        [Column(CanBeNull = true)]
        public string Value;
    }

    public class TableCreator
    {
        protected string connStr { get { return ConfigurationManager.ConnectionStrings["conn"].ConnectionString; } }

        public void Create()
        {
            var ctx = new DataContext(connStr);

            if (ctx.DatabaseExists())
            {
                ctx.DeleteDatabase();
            }
            else
            {
                ctx.GetTable<TestTable>();
                ctx.CreateDatabase();
            }

            var table = ctx.GetTable<TestTable>();
            var tbnew = new TestTable { /*Id = 1, */Key = "TestKey", Value = "TestValue" };
            table.InsertOnSubmit(tbnew);
            ctx.SubmitChanges();
        }
    }
}
